package MainFunctionsDAOS;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import module4.MySQLConnection;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;


public class Table extends JPanel
{
	private ResultSetMetaData resMet = null;
	private Vector dataVect = null;
	private Vector headingVect = null;
	private Vector stockVect = null;
	
	private DefaultTableModel searchModel = null;
	
	
	public JTable createStockSearchTable(ResultSet rs)
	{
		JTable table = null;
		try
		{
			resMet = rs.getMetaData();
			searchModel = new DefaultTableModel(){
   				public boolean isCellEditable(int row, int column)
				{
				    return false;
				}
				 };
			dataVect = new Vector(createDataVect(rs));
			headingVect = new Vector(createHeadingVect(rs));
			searchModel.setDataVector(dataVect, headingVect);
			table = new JTable(searchModel);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			table.setAutoCreateRowSorter(true);
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		return table;		
	}
	
	public Vector createDataVect(ResultSet rs) throws SQLException
	{
		Vector vect = new Vector();
		while(rs.next())
		{
			Vector innerVect = new Vector();
			int i = 1;
			while(i <= resMet.getColumnCount())
			{
				innerVect.add(rs.getString(i));
				i++;
			}
			vect.add(innerVect);
		}
		return vect;
	}
	
	public Vector createHeadingVect(ResultSet rs) throws SQLException
	{
		
		Vector vect = new Vector(resMet.getColumnCount());
		int i = 1;
		while(i <= resMet.getColumnCount())
		{
			vect.add(resMet.getColumnName(i));
			i++;
		}
		
		return vect;
	}
	
	public DefaultTableModel getDefaultModel()
	{
		return searchModel;
	}
}
